<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
$return['msgtypes']=array('0'=>'文本','1'=>'图片','2'=>'语音','3'=>'视频','4'=>'图文');
if($_RQ['op']=='list'){
	$type=isset($_RQ['type'])?intval($_RQ['type']):4;
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>$type);
	if(!empty($_RQ['wxgid'])){
		$params['wxgid']=intval($_RQ['wxgid']);
		$return['params']['wxgid']=$_RQ['wxgid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 24;
	$total='';
	$list=pdo_getslice('core_media',$params,array($pindex,$psize),$total,array('id','type','lookcontent','attachment','title','createtime'),'id',$orderby);
	foreach($list as &$item){
		if($item['type']){
			$item['lookcontent']=json_decode($item['lookcontent'],true);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['type']=$type;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='get'){
	//同步微信素材
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$wechat=intval($postdata['wxgid']);
		if(!$wechat){
			exi('没有选择公众号！','error');
		}
		$type=intval($postdata['type']);
		cfc('account',$wechat)->get_medialist($type,$wechat);
		$types=array(
			'1'=>'image',
			'2'=>'audio',
			'3'=>'video',
			'4'=>'news',
		);
		exi('同步成功','',$types[$type]);
	}
	$types=array(
		'1'=>'图片',
		'2'=>'语音（音乐）',
		'3'=>'视频',
		'4'=>'图文',
	);
	$return['item']=array();
	$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'type'=>array('2','4')),array('id','title'));
	$return['types']=$types;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'image','title'=>'图片'),
			array('op'=>'voice','title'=>'语音'),
			array('op'=>'video','title'=>'视频'),
			array('op'=>'news','title'=>'图文'),
			array('op'=>'get','title'=>'同步微信素材'),
			array('op'=>'add','title'=>'新增素材'),
		),
		'desc'=>'设置同步素材信息',
		'edit'=>array(
			array('type'=>'select','name'=>'wxgid','title'=>'公众号','options'=>'wechats'),
			array('type'=>'radio','name'=>'type','title'=>'素材类型','options'=>'types'),

		),
	);
	exi($return);
}

if($_RQ['op']=='add'){
	//新增素材
	$send_to_types=array(
		//'1'=>'发送给指定粉丝',
		'2'=>'发送给指定分组的粉丝',
		'3'=>'发送给所有粉丝',
	);
	$send_status=array(
		'0'=>'待发送',
		'1'=>'已发送',
	);
	$content_types=array(
		'0'=>'文本',
		'1'=>'图片',
		'2'=>'语音（音乐）',
		'3'=>'视频',
		'4'=>'图文',
	);
	$choose_types=array(
		'1'=>'本地素材选取',
		'2'=>'从公众号素材库选取',
	);
	$id=intval($_RQ['id']);
	$type=intval($_RQ['type']);
	$wxgid=intval($_RQ['wxgid']);
	if($id){
		$item=pdo_get('core_media',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['send_time']=date('Y/m/d H:i',$item['send_time']);
		if(!$_SESSION['wechat_id']){
			$_SESSION['wechat_id']=$item['wxgid'];
		}
		if($item['type']!=0){
			$item['lookcontent']=$item['lookcontent']?json_decode($item['lookcontent'],true):array();
		}
		if($item['type']==1){
			$item['content']=$item['content']?json_decode($item['content'],true):array();
		}
	}else{
		if(!$wxgid){
			$wxgid=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1),'id');
			$wxgid=$wxgid?:pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),'id');
		}
		$_SESSION['wechat_id']=$wxgid;
		$item=array(
			'wechat_title'=>pdo_getcolumn('core_account_wechat',array('id'=>$_SESSION['wechat_id'],'uniacid'=>$_SESSION['uniacid']),'title'),
			'edit'=>1,
			'wxgid'=>$wxgid,
			'type'=>4,
			'content'=>array(),
			'lookcontent'=>array(),
		);
	}
	$return['item']=$item;
	$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'type'=>array('2','4')),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='post'){
	//新增素材
	$postdata=get_postdata();	
	$wechat=intval($postdata['wxgid']);
	$id=intval($postdata['id']);
	$lookcontent=$postdata['lookcontent'];
	if(!$postdata['title']){
		exi('请输入标题！','error');
	}
	if(!$lookcontent){
		exi('素材内容不能为空！','error');
	}
	if(!$wechat){
		exi('没有选择公众号！','error');
	}
	$type=intval($postdata['type']);
	$wechat=cfc('account',$wechat);
	$media_ids=array();
	if($type==1 || $type==2 || $type==3){
		foreach($lookcontent as $cv){
					if($type==1 || $type==2){//图片和语音
						$media_id=$wechat->add_media($cv,$type,$postdata['title']);
					}
					if($type==3){
						$media_id=$wechat->add_media($cv,$type,$postdata['title'],$postdata['content_title'],$postdata['content_desc']);
					}
					if(is_error($media_id)){
						exi('上传素材出错：'.$media_id['message'],'error');
					}
					$media_ids[]=$media_id;
		}
	}elseif($type==4){
		$media_ids=$wechat->add_media_news($lookcontent,$postdata['title'],$id);
	}
	if($type==1){//只有图片的多个media_ids
		$postdata['content']=ijson_encode($media_ids);
	}elseif($type!=0){
		$postdata['content']=$media_ids[0];
	}
	if($type){//只有文本的不需要整理
		$postdata['lookcontent']=ijson_encode($lookcontent);
	}else{
		$postdata['content']=$lookcontent;
	}
	$updata=array(
			'title'=>$postdata['title'],
			'lookcontent'=>$postdata['lookcontent'],
			'content'=>$postdata['content'],
	);
	if($id){
		pdo_update('core_media',$updata,array('id'=>$id));
		exi('修改素材成功！','','/wechat.media/list/type='.$type);		
	}else{
		$updata['uniacid']=$_SESSION['uniacid'];
		$updata['type']=$type;
		$updata['createtime']=TIMESTAMP;
		pdo_insert('core_media',$updata);
		exi('新增素材成功！','','/wechat.media/list/type='.$type);		
	}	
}
if($_RQ['op']=='get_article'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']=trim($_RQ['title']);
		$return['params']['title']=$_RQ['title'];
	}
	$pindex=intval($_RQ['page'])?:1;
	$psize=10;
	$fields=array('id','title','pic_url');
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['res']=array(
				'id'=>$item['id'],
				'title'=>$item['title'],
				'image'=>$item['pic_url'],
			);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
		),'action'=>array(
			array('name'=>'res','title'=>'选取','type'=>'modal_result'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='ctitle'){
	//编辑名称
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$id=intval($postdata['id']);
		if(!$id){
			exi('请先选择附件1！','error');
		}
		$postdata=get_postdata();
		$filename=trim($postdata['title']);
		if($filename){
			pdo_update('core_media',array('title'=>$filename),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('附件名称修改成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择附件！','error');
	}
	$return=array();
	$return['item']=pdo_get('core_media',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改附件名称',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'附件名称'),
		),
	);
	exi($return);
}
if($_RQ['op']=='media_news'){
	//图文素材文章管理
	$id=intval($_RQ['id']);
	if($id){
		$_SESSION['media_news_id']=$id;
		$_SESSION['media_news_wxgid']=pdo_getcolumn('core_media',array('id'=>$_SESSION['media_news_id'],'uniacid'=>$_SESSION['uniacid']),'wxgid');
	}
	if(!$_SESSION['media_news_id']){
		exi('请先选择要管理的图文！','error');
	}
	$list=pdo_getall('core_media_news',array('media_id'=>$_SESSION['media_news_id'],'uniacid'=>$_SESSION['uniacid']),array('id','title','thumb','url'));
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'news','title'=>'返回图文素材列表'),
			array('op'=>'media_news','title'=>'素材文章列表'),
		),
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'pagelist'=>false,'edit_op'=>'news_edit','list'=>array(
			array('type'=>'string','name'=>'title','title'=>'文章标题'),
			array('type'=>'image','name'=>'thumb','title'=>'文章封面'),
			array('type'=>'ewm','name'=>'url','title'=>'预览'),
		)),
	);
	exi($return);
}
if($_RQ['op']=='news_edit'){
	//图文素材文章编辑
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>$post['title'],
			'index'=>$post['index'],
			'thumb'=>$post['thumb'],
			'show_cover_pic'=>intval($post['show_cover_pic']),
			'author'=>$post['author'],
			'digest'=>$post['digest'],
			'content'=>$post['content'],
			'content_source_url'=>$post['content_source_url'],
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('core_media_news',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$api_url='https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={access_token}';
			$params=array(
				'media_id'=>pdo_getcolumn('core_media',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['media_news_id']),'media_id'),
				'index'=>$data['index'],
				'articles'=>array(
					'title'=>$data['title'],
					'thumb_media_id'=>pdo_getcolumn('core_media',array('uniacid'=>$_SESSION['uniacid'],'attachment'=>$data['thumb']),'media_id'),
					'show_cover_pic'=>$data['show_cover_pic'],
					'author'=>$data['author'],
					'digest'=>$data['digest'],
					'content'=>$data['content'],
					'content_source_url'=>$data['content_source_url'],
				),
			);
			$params=ijson_encode($params);
			cfc('account')->request($api_url,$params);
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['wxgid']=$_SESSION['media_news_wxgid'];
			$data['media_id']=$_SESSION['media_news_id'];
			pdo_insert('core_media_news',$data);
			
		}
		exi('保存成功！','','media_news');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_media_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array('url'=>'');
	}
	$return['item']=$item;
	$return['wechat_image_url']=true;//指明使用微信图片地址
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'media_news','title'=>'返回文章列表'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'文章标题'),
			array('type'=>'ewm','name'=>'url','title'=>'预览二维码','show'=>array('id >'=>0,'url !='=>'')),
			array('type'=>'image','name'=>'thumb','title'=>'文章封面'),
			array('type'=>'switch','name'=>'show_cover_pic','title'=>'是否显示封面'),
			array('type'=>'string','name'=>'author','title'=>'作者'),
			array('type'=>'string','name'=>'digest','title'=>'摘要'),
			array('type'=>'richtext','name'=>'content','title'=>'文章内容'),
			array('type'=>'string','name'=>'content_source_url','title'=>'原文地址'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	//删除素材
	$del=str_array($_RQ['del']);
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(!$del){
		exi('请先选择要删除的素材！','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('core_media',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('删除成功！');
}